'use strict';
const db = uniCloud.database()
exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('event : ', event)
	let code=event.code
	
	let u='https://api.weixin.qq.com/sns/jscode2session?appid=wxc7fe3adadeaa9875&secret=1f18e26bdfef693477ac23b3c24efb54&js_code='+code+'&grant_type=authorization_code'
	let res = await uniCloud.httpclient.request(
		u, // 请求路径,
		{
			dataType: "json"
		}
	);
	console.log(res.data.openid)
	// console.log('event : ', event)
	let status=2
	const collection = db.collection('User')
	const res2 = await collection.get()
	let users=res2.data
	for(let i=0;i<users.length;i++){
		if(users[i].user_name==res.data.openid){
			status=-1
			break
		}
	}
	
	//返回数据给客户端
	return {
		"status":status,
		"openid":res.data.openid
	}
	//返回数据给客户端
	// return res
};
